#!/bin/bash
#----------------------------------------------------------------------------#
#   veget - downloads and installs a virtual environment
#   Copyright (C) 2010 Paul Andrew Liljenberg 
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------#

#config-----------------------------------------------------------------------#
SLEEPTIME="5"
LOGFILE=/tmp/$NAME-$USER.log
BRNAME="br0"
EXT="kvm-amd"
PRIV="755"
#-----------------------------------------------------------------------------#

#appspecific variables--------------------------------------------------------#
NAME="veget"
VERSION="1.0"
TMPPATH=/tmp/$NAME-$USER
#-----------------------------------------------------------------------------#

#virtual environment specific variables---------------------------------------#
EMULATOR="qemu-kvm-0.12.5"
VE="http://sourceforge.net/projects/kvm/files/qemu-kvm/0.12.5/qemu-kvm-0.12.5.tar.gz/download"
QEMUIFUP="http://veget.googlecode.com/hg/qemu-ifup"
INTERFACES="http://veget.googlecode.com/hg/interfaces"
#------------------------------------------------------------------------------#

#----------------------------------------------------------------------------------------------------#
echo -e "Running $NAME v$VERSION @ $(date)"
function apt(){
        echo -e "\e[01;32m[>]\e[00m Upgrading and installs necessary packages."
        if  xdpyinfo >> $LOGFILE
then
        xterm -geometry 100x15+10+110 -T "$NAME v$VERSION: Upgrading and installs necessary packages..." -e /bin/bash -c \
        "apt-get -q update && apt-get -q  upgrade -y && apt-get -q install uml-utilities bridge-utils libpci-dev libsdl1.2-dev -y && sleep $SLEEPTIME;"
else
        apt-get -q update && apt-get -q  upgrade -y && apt-get -q install uml-utilities bridge-utils libpci-dev libsdl1.2-dev -y
fi

        if [ "$?" -ne "0" ]
then
        echo "Installation failed, exiting.";
        exit
fi
}
#---------------------------------------------------------------------------------------------------#

function fetch(){
        echo -e "\e[01;32m[>]\e[00m Fetching necessary files."
        if xdpyinfo >> $LOGFILE
then
        xterm -geometry 100x15+10+110 -T "$NAE v$VERSION: Fetching necessary files..." -e /bin/bash -c \
        "mkdir -p  $TMPPATH && cd $TMPPATH && wget $VE $QEMUIFUP $INTERFACES && sleep $SLEEPTIME";
else
        mkdir -p $TMPPATH && cd $TMPPATH && wget $VE $QEMUIFUP $INTERFACES && sleep $SLEEPTIME
fi
        if [ "$?" -ne "0" ]
then
        echo "wget failed, exiting.";
        sleep $SLEEPTIME;
        exit
fi
}
#-----------------------------------------------------------------------------------------------------#

function cleanup(){
#now cleanup!
        echo -e "\e[01;32m[>]\e[00m Cleanup."
        rm -rf $TMPPATH
        apt-get -qq autoclean
}
#-----------------------------------------------------------------------------------------------------#
function backup(){
        echo -e "\e[01;32m[>]\e[00m Backing up files."
        mv /etc/network/interfaces.bak /etc/network/interfaces >> $LOGFILE
}
#-----------------------------------------------------------------------------------------------------#
function network(){

        echo -e "\e[01;32m[>]\e[00m Setup network environment."
        mv /etc/network/interfaces /etc/network/interfaces.bak >> $LOGFILE
        mv $TMPPATH/interfaces /etc/network/interfaces >> $LOGFILE
        chmod $PRIV $TMPPATH/qemu-ifup && mv $TMPPATH/qemu-ifup /etc
        echo -e "\e[01;32m[>]\e[00m Adding bridge."
        brctl addbr $BRNAME >> $LOGFILE
        brctl addif $BRNAME eth0 >> $LOGFILE
        echo -e "\e[01;32m[>]\e[00m Applying vlanfix."
        cd /proc/sys/net/bridge/
        for f in bridge-nf-*; do echo 0 > $f; done
        echo -e "\e[01;32m[>]\e[00m Restarting network."
        xterm -geometry 100x15+10+110 -T "$NAME v$VERSION: Restarting network." -e /bin/bash -c \
        "/etc/init.d/networking restart | tee -a $LOGFILE && /etc/init.d/networking restart >/dev/null 2>&1";

}
#---------------------------------------------------------------------------------------------------#
function install(){
        echo -e "\e[01;32m[>]\e[00m Installing environment."
        if xdpyinfo >> $LOGFILE
then
        xterm -geometry 100x15+10+110 -T "$NAME v$VERSION: Unpackageing " -e /bin/bash -c \
        "cd $TMPPATH && tar -xvvf $TMPPATH/$EMULATOR.tar.gz && sleep $SLEEPTIME";
        echo -e "\e[01;32m[>]\e[00m Unpackageing done."

        xterm -geometry 100x15+10+110 -T "$NAME v$VERSION: Compiling" -e /bin/bash -c \
        "cd $TMPPATH/$EMULATOR/ && ./configure --enable-sdl && make && sleep $SLEEPTIME";
        echo -e "\e[01;32m[>]\e[00m Compiling done."

        xterm -geometry 100x15+10+110 -T "$NAME v$VERSION: Installing" -e /bin/bash -c \
        "cd $TMPPATH/$EMULATOR/ && make install && sleep $SLEEPTIME";
        echo -e "\e[01;32m[>]\e[00m Installation done."
else
        cd $TMPPATH/$EMULATOR/ && ./configure --enable-sdl && make make install
        echo -e "\e[01;32m[>]\e[00m Installation done."


fi
modprobe kvm && modprobe $EXT

}

#main---------------------------------------------------------------------------------------------#

backup;apt;fetch;network;install;cleanup
